//
//  ZLMoreViewController.m
//  TripTogether
//
//  Created by dllo on 15/12/26.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "ZLMoreViewController.h"
#import "WJQNetwork.h"
#import "ZLMoreCollectionViewCell.h"
#import "ZLDataMOdel.h"
#import "ZLDetailViewController.h"


@interface ZLMoreViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong)UICollectionView *myCollectionView;
@property (nonatomic, strong)NSMutableArray *myArray;

@end

@implementation ZLMoreViewController

#pragma mark - 隐藏tabbar
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [super viewWillAppear:animated];
    NSArray *temp = self.tabBarController.view.subviews;
    UIView *view = temp[2];
    view.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myArray = [NSMutableArray array];
    self.navigationItem.title = self.titles;
    
    [self createCollectionView];
    [self getData];
    
}

#pragma mark - 创建CollectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    flowLayout.sectionInset = UIEdgeInsetsMake(5*AUTO_HEIGHT, 10*AUTO_WIDTH, 10*AUTO_HEIGHT, 10*AUTO_WIDTH);
    
    //行 列 间距
    flowLayout.minimumLineSpacing = 5*AUTO_HEIGHT;
    flowLayout.minimumInteritemSpacing = 5*AUTO_WIDTH;
    flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 27*AUTO_WIDTH)/2, (SCREEN_WIDTH - 25*AUTO_WIDTH)/2);
    
    self.myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.backgroundColor = [UIColor colorWithRed:0.98 green:0.96 blue:0.91 alpha:1];
    [self.view addSubview:self.myCollectionView];
    
    [self.myCollectionView registerClass:[ZLMoreCollectionViewCell class] forCellWithReuseIdentifier:@"ZLMoreCollectionViewCell"];
    [self.myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter"];
}

#pragma mark - CollectionView协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.myArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZLMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZLMoreCollectionViewCell" forIndexPath:indexPath];
    
    cell.zlMoreM = [self.myArray objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark - 网络请求
- (void)getData
{
    //http://api.breadtrip.com/destination/index_places/3/
    NSString *urlStr = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/index_places/%ld/",self.index];
    [WJQNetwork getAFNet:urlStr block:^(id result) {
        NSMutableDictionary *dic = result;
        
        NSMutableArray *arr = [dic objectForKey:@"data"];
        for (NSMutableDictionary *Dic in arr) {
            ZLDataMOdel *zlMoreM = [ZLDataMOdel new];
            [zlMoreM setValuesForKeysWithDictionary:Dic];
            [self.myArray addObject:zlMoreM];

        }
        [self.myCollectionView reloadData];
        
    } errorBlock:^(NSError *error) {
        
        NSLog(@"网络请求失败");
    }];
}

#pragma mark - 尾部
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionFooter){
        UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter" forIndexPath:indexPath];
        
        return reusableView;
    }else{
        return nil;
    }
}

#pragma mark - 返回尾部高度

//尾部高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    
    return CGSizeMake(SCREEN_WIDTH, 55);

}

#pragma mark - 点击 触发事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZLDetailViewController *zlDetailVC = [ZLDetailViewController new];
    zlDetailVC.zlDataM = [self.myArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:zlDetailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
